﻿using System;
using System.Runtime.InteropServices;
using EasyHook;
using EasyHookSandBox.Injector.Ipc;

namespace EasyHookSandBox.Injector
{
    public class HookEntryPoint : IEntryPoint
    {
        private LocalHook _hook;
        private readonly HookChannel _channel;

        public HookEntryPoint(RemoteHooking.IContext context, string inChannelName)
        {
            _channel = RemoteHooking.IpcConnectClient<HookChannel>(inChannelName);
        }

        public void Run(RemoteHooking.IContext context, string inChannelName)
        {
            try
            {

                //var prcAdr = LocalHook.GetProcAddress("notepad.exe", "ShowHelp");
                //_hook = LocalHook.Create(prcAdr, new ShowHelp(ShowHelpHook), this);


               // _hook.ThreadACL.SetExclusiveACL(new[] {0});

            }
            catch (Exception ex)
            {
                _channel.ReportException(ex);
            }

            _channel.IsInstalled(RemoteHooking.GetCurrentProcessId());
        }

        static IntPtr ShowHelpHook()
        {
            var instance = (HookEntryPoint) HookRuntimeInfo.Callback;
            instance._channel.OnHelpShow();

            return IntPtr.Zero;
        }

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
        private delegate IntPtr ShowHelp();
    }
}
